﻿using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Driver;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.Entities;

namespace Sustainalytics.DataLayer
{
    public class SubscribeToPortfolioRepository : RepositoryCollection<SubscribeToPortfolio>, ISubscribeToPortfolioRepository
    {
        public SubscribeToPortfolioRepository(string cnxString) : base(cnxString)
        {
        }

        public static ISubscribeToPortfolioRepository GetInstance(string connectionString)
        {
            return new SubscribeToPortfolioRepository(connectionString);
        }

        public IMongoDatabase GetDb() {
            return Collection().Database;
        }

        public List<SubscribeToPortfolio> GetSubscriptions(Guid userId, Guid accountId)
        {
            return ReadAllWhere(s => s.UserId == userId && s.AccountId == accountId).ToList();
        }

        public SubscribeToPortfolio ReadById(Guid id)
        {
            return Read(id);
        }

        public void Insert(SubscribeToPortfolio subscribe)
        {
            Create(subscribe);
        }

        public void Update(SubscribeToPortfolio subscribe)
        {
            base.Update(subscribe.Id, subscribe);
        }

        public void UpdateStatus(Guid id, bool status)
        {
            var updateSubscription = Builders<SubscribeToPortfolio>.Update.Set(p => p.Enabled, status);
            GetDb().GetCollection<SubscribeToPortfolio>("SubscribeToPortfolio").UpdateOne(Builders<SubscribeToPortfolio>.Filter.Where(p => p.Id == id), updateSubscription);
        }
    }
}
